package com.coder4j.platform.controller;

import com.coder4j.platform.cache.RoleCache;
import com.coder4j.starter.mybatis.BaseEntity;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.util.ObjectUtils;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.DataScopeEnums;
import com.coder4j.starter.web.OptionVO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.platform.annotation.OperationLog;
import com.coder4j.platform.entity.AuthRoleEntity;
import com.coder4j.platform.service.AuthRoleService;
import com.coder4j.platform.vo.AuthRoleEO;
import com.coder4j.platform.vo.AuthRoleQuery;
import com.coder4j.platform.vo.AuthRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色
 *
 * @author keller
 * @date 2024-05-07 17:51:15
 */
@Slf4j
@RestController
@RequestMapping("/authRole")
public class AuthRoleController {
    @Resource
    private AuthRoleService service;

    @Resource
    private HttpServletResponse response;

    @GetMapping("/getDataScopeTypeList")
    @ApiNote(name = "查询数据范围类型", createName = "keller", createTime = "2024-05-07 17:51:15")
    public R getDataScopeTypeList() {
        return R.success(DataScopeEnums.getOptions());
    }

    /**
     * 按ID查询角色
     *
     * @param id AuthRole.id
     * @return 角色对象
     */
    @GetMapping("/getById")
    @ApiNote(name = "按ID查询角色", createName = "keller", createTime = "2024-05-07 17:51:15")
    public R<AuthRoleVO> getById(Long id) {

        log.info("====== AuthRoleControllerGetById ====== {}", id);
        return R.success(service.getDetail(id));
    }

    /**
     * 保存角色
     * 有 id 字段，更新；否则新增
     *
     * @param authRole AuthRole对象
     * @return AuthRole对象
     */
    @PostMapping("/save")
    @OperationLog("保存角色")
    @ApiNote(name = "保存角色", createName = "keller", createTime = "2024-05-07 17:51:15")
    public R save(@RequestBody AuthRoleVO authRole) {
        log.info("====== AuthRoleControllerSave ====== {}", authRole);

        if (ObjectUtils.hasEmpty(authRole.getName())) {
            return R.paramError();
        }
        //内置超管角色，不允许修改
        if (authRole.getId() != null && authRole.getId() == 0) {
            return R.noAuth();
        }
        service.baseSaveVO(authRole);
        RoleCache.initCache();
        return R.success();
    }

    /**
     * 查询列表
     *
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
//    @OperationLog("查询角色列表")
    @ApiNote(name = "查询角色列表", createName = "keller", createTime = "2024-05-07 17:51:15")
    public R<BasePage<AuthRoleVO>> getList(@RequestBody AuthRoleQuery query) {
        if (query == null) {
            query = new AuthRoleQuery();
        }
        log.info("====== AuthRoleControllerGetList ====== {}", query);
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     *
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name = "导出角色列表", createName = "keller", createTime = "2024-05-07 17:51:15")
    public void export(@RequestBody AuthRoleQuery query) {
        if (query == null) {
            query = new AuthRoleQuery();
        }
        log.info("====== AuthRoleControllerGetList ====== {}", query);
        List<AuthRoleEntity> list = service.baseQueryList(query);
        List<AuthRoleEO> eoList = new ArrayList<>();
        for (AuthRoleEntity entity : list) {
            AuthRoleEO eo = new AuthRoleEO();
            BeanUtils.copyProperties(entity, eo);
            eoList.add(eo);
        }
        EasyExcelUtils.exportClass(response, "AuthRoleList", eoList);
    }


    /**
     * 删除角色
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name = "删除角色", createName = "keller", createTime = "2024-05-07 17:51:15")
    public R deleteById(@RequestBody BaseEntity vo){
        if(vo.getId() == null){
            return R.paramError();
        }
        //内置超管角色，不允许删除
        if (vo.getId() == 0) {
            return R.noAuth();
        }
        service.baseDeleteById(vo.getId());
        RoleCache.initCache();
        return R.success();
    }

    @PostMapping("/setRight")
    @ApiNote(name = "为角色设置权限", createName = "keller", createTime = "2024-5-15 16:39:49")
    public R setRight(@RequestBody AuthRoleVO vo) {
        //角色ID必填，且不能是内置角色
        if(vo.getId() == null || vo.getId() <= 0){
            return R.paramError();
        }
        service.setRight(vo);
        RoleCache.refreshRoleApiCache(vo.getId());
        return R.success();
    }

    @GetMapping("/getSelectorOptions")
    @ApiNote(name = "获取角色的下拉选项",createName = "keller",createTime = "2024-5-16 11:19:11")
    public R getSelectorOptions(){
        List<OptionVO<Long>> list = RoleCache.getSelectorOptions();
        return R.success(list);
    }
}
